package myself.study.network.udp.examples;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
* @Name: UdpClient
* @Description: udp案例-聊天程序客户端，请求数据由键盘手工录入
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-1-12 上午9:51:18 
* @Version: V1.0
 */
public class UdpClient {
	
	public static void main(String[] args) throws IOException {
		//创建发送数据的Socket对象
		DatagramSocket client = new DatagramSocket() ;
		//封装键盘录入数据
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)) ;
		String line = null ;
		while((line = reader.readLine()) != null) {
			//当键盘输入'886'时，结束客户端请求
			if("886".equals(line)) {
				break ;
			}
			//创建发送数据包
			byte[] buf = line.getBytes() ;
			//使用固定ip地址
//			DatagramPacket packet = new DatagramPacket(buf, 
//													   buf.length, 
//													   InetAddress.getByName("222.28.133.3"), 
//													   12345) ;
			//使用广播地址
			DatagramPacket packet = new DatagramPacket(buf, 
					   buf.length, 
					   InetAddress.getByName("222.28.133.255"), 
					   12345) ;
			//发送数据包
			client.send(packet) ;
		}
		//释放资源
		client.close() ;
	}

}
